import os
import sys
d = os.path.dirname(__file__)
parent_path = os.path.dirname(d)
sys.path.append(parent_path)
from ulits import fstool

class User(object):    
    def __init__(self ,user_list):
        self.user_list=user_list
        self.path = parent_path+'/data'
    def enter(self):#登录
        a = 0
        while a<=2:
            user_name = input('请输入用户名：')
            user_pwd = input('请输入密码：')
            for i in self.user_list:
                if user_name == str(i[1]) and user_pwd == str(i[2]):
                    print('登录成功！')
                    return i[0]
            else:
                a+=1
                print('用户名或密码不正确，你还有%d次机会！'%(3-a))
        else:
            exit('请申请后再来登录！')
    def logon(self,file_name,sheet_name):#注册
        sss = []
        log = []
        for i in self.user_list:
            sss.append(i[0])
        while True:
            ss = input('请输入用户ID：')
            if len(ss) == 11 and ss not in sss:
                log.append(ss)
                break
            else:
                print('输入的ID不对！')
        for i in self.user_list[0][1:]:
            ss = input('请输入%s：'%i)
            log.append(ss)
        a=fstool.Excel(self.path + '\\' + file_name)
        a.write_row_data(log,sheet_name)

def amend_user(user_id ,user_xls ,user_sheet):
    excel_user = fstool.Excel(parent_path+'/data/' + user_xls)
    user_list = excel_user.read_all_data(user_sheet)[0]
    while True:
        A = ['修改用户名','修改密码','返回','退出']
        qq = fstool.show(A)
        if qq == '1':
            uname = input('请输入新的用户名：')
            obj = {user_list[0]:user_id ,user_list[1]:uname}
            excel_user.write_cell_data(obj ,user_sheet)
        elif qq == '2':
            upwd = input('请输入新的密码：')
            obj = {user_list[0]:user_id ,user_list[2]:upwd}
            excel_user.write_cell_data(obj ,user_sheet)
        elif qq == '3':
            break
        elif qq == '4':
            exit('退出')
        else:
            print('输入错误！')

def user_login(user_xls ,user_sheet):#登录界面
    while True:
        path = parent_path+'/data/'
        excel_user = fstool.Excel(path + user_xls)
        user_list = excel_user.read_all_data(user_sheet)
        use = User(user_list)
        A=['登录','注册','返回','退出']
        s0 = fstool.show(A) 
        if s0 == '1':
            return use.enter()            
        elif s0 == '2':
            use.logon(user_xls ,user_sheet)
        elif s0 == '3':
            return True
        elif s0 == '4':
            exit('退出')
        else:
            print('输入错误！')

if __name__ == '__main__':
    amend_user('12345678901' ,'buyer.xls' ,'buyer')

    
